package edu.ahpu.propertymanage.controller;

import edu.ahpu.propertymanage.dataclass.TageForm;
import edu.ahpu.propertymanage.pojo.Managers;
import edu.ahpu.propertymanage.pojo.PropertyTage;
import edu.ahpu.propertymanage.service.ManagersService;
import edu.ahpu.propertymanage.service.PropertyTageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/tage")
public class PropertyTageController {

    @Autowired
    PropertyTageService propertyTageService;

    @Autowired
    ManagersService managersService;

    @PostMapping("settage")
    public String setTage(@RequestBody TageForm tageForm) {
        PropertyTage propertyTage = new PropertyTage();
        propertyTage.setId(1);
        propertyTage.setTage(tageForm.getMsg());
        boolean bool = propertyTageService.updateById(propertyTage);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("setprice")
    public String setWaterPrice(@RequestBody TageForm tageForm, Integer tage) {
        PropertyTage propertyTage = new PropertyTage();
        switch (tage) {
            case 2:
                propertyTage.setId(2);//水费
                break;
            case 3:
                propertyTage.setId(3);//电费
                break;
            case 4:
                propertyTage.setId(4);//燃气费
                break;
            case 5:
                propertyTage.setId(5);//物业费
                break;
            case 6:
                propertyTage.setId(6);
                break;
        }
        propertyTage.setPrice(Float.valueOf(tageForm.getMsg()));
        boolean bool = propertyTageService.updateById(propertyTage);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("managepow")
    public String  managePow(@RequestBody TageForm tageForm){
        List<PropertyTage> propertyTageList = propertyTageService.list();
        int count=0;
        boolean bool=false;
        for (PropertyTage p:propertyTageList) {
            if ("".equals(p.getPrice())||p.getPrice()==null)
                count++;
        }
        if (count==1){
            Managers managers = managersService.getById(tageForm.getId());
            managers.setManagerPow(4);
            bool=managersService.updateById(managers);
        }
        if (bool)
            return "success";
        else
            return "error";
    }

}
